#include"process.h"
#include<unistd.h>
#include<time.h>
#include<stdlib.h>

//函数指针类型 
typedef void (*call_t)(const char*,double,double);

double total =1024.0;
//double speed=1.0;
double speed[]={10.0,0.8,0.3,0.2,0.01,0.001};

//回调函数
void Download(int total,call_t cb)
{
   srand(time(NULL));
   double current = 0.0;
   while(current <= total)
   {
      cb("下载中",total,current);//进行回调
      if(current>=total) break;

      //下载代码逻辑
      int random=rand()%6;

      usleep(5000);
      current += speed[random];

      if(current>=total) current=total; 
   }
}

//回调函数
void uploadload(int total,call_t cb)
{
   srand(time(NULL));
   double current = 0.0;
   while(current <= total)
   {
      cb("上传中",total,current);//进行回调
      if(current>=total) break;

      //下载代码逻辑
      int random=rand()%6;

      usleep(5000);
      current += speed[random];

      if(current>=total) current=total; 
   }
}


int main()
{
   Download(1024.0,FlushProcess);
   printf("download 1024.MB done\n");
   Download(512.0,FlushProcess);
   printf("download 512.MB done\n");
   Download(256.0,FlushProcess);
   printf("download 256.MB done\n");
   Download(128.0,FlushProcess);
   printf("download 128.MB done\n");
   Download(64.0,FlushProcess);
   printf("download 64.MB done\n");
   uploadload(512.0,FlushProcess);
//   process();
   printf("uploadload 64.MB done\n");
   return 0;
}
